package com.health.home.service.lmpl;

import com.health.common.core.utils.StringUtils;
import com.health.home.domain.po.TmedicinePo;
import com.health.home.domain.vo.TmedicineVo;
import com.health.home.mapper.TmedicineMapper;
import com.health.home.service.TmedicineService;
import com.health.home.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xpz
 * @date 2024/12/11 22:32
 */
@Service
@Slf4j
public class TmedicineServicelmpl implements TmedicineService{

    @Autowired
    private TmedicineMapper tmedicineMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;


    private static final String CACHE_KEY_PREFIX = "medicineId:";
    @Override
    public List<TmedicinePo> temdicineList() {
        List<TmedicinePo> list = tmedicineMapper.temdicineList();
        return list;
    }

    @Override
    public List<TmedicinePo> pharmaceuticalsLists(TmedicineVo tmedicineVo) {
        List<TmedicinePo> list = tmedicineMapper.pharmaceuticalsLists(tmedicineVo);
        return list;
    }

    @Override
    public Result pharmaceuticalsAdd(TmedicinePo tmedicinePo) {
        if (StringUtils.isEmpty(tmedicinePo.getMedicineName())){
            return Result.error("药品名称不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getMedicineImagePath())){
            return Result.error("药品图片不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getIngredients())){
            return Result.error("药品成分不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getContraindications())){
            return Result.error("用药禁忌不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getIndications())){
            return Result.error(" 功能主治不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getDosageInstructions())){
            return Result.error("用法用量不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getMedicineCharacteristics())){
            return Result.error("药品特性不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getPackagingSpecification())){
            return Result.error("包装规格不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getAdverseReactions())){
            return Result.error("不良反应不能为空");
        }
        tmedicineMapper.pharmaceuticalsAdd(tmedicinePo);
        return Result.success();
    }

    @Override
    public Result pharmaceuticalsUpdate(TmedicinePo tmedicinePo) {
        if (StringUtils.isEmpty(tmedicinePo.getMedicineName())){
            return Result.error("药品名称不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getMedicineImagePath())){
            return Result.error("药品图片不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getIngredients())){
            return Result.error("药品成分不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getContraindications())){
            return Result.error("用药禁忌不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getIndications())){
            return Result.error(" 功能主治不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getDosageInstructions())){
            return Result.error("用法用量不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getMedicineCharacteristics())){
            return Result.error("药品特性不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getPackagingSpecification())){
            return Result.error("包装规格不能为空");
        }
        if (StringUtils.isEmpty(tmedicinePo.getAdverseReactions())){
            return Result.error("不良反应不能为空");
        }
        tmedicineMapper.pharmaceuticalsUpdate(tmedicinePo);
        return Result.success();
    }

    @Override
    public void pharmaceuticalsDel(Integer id) {

        String cacheKey=CACHE_KEY_PREFIX+id;
        //第一次删除缓存 在操作数据库之前
        redisTemplate.delete(cacheKey);
        try {
            int i = tmedicineMapper.removeByIdDelete(id);
            if (i>0){
                // 第二次删除缓存（在数据库操作之后）
                redisTemplate.delete(cacheKey);
                log.info("药品信息删除成功: {}", id);
            }else {
                log.warn("药品信息删除失败，未找到要删除的记录: {}", id);
            }
        } catch (Exception e) {
            log.error("药品信息删除失败，错误信息：{}", e.getMessage(), e);

        }
    }
}
